//
//  GameCenterListCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/5.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SDWebImage

class GameCenterListCell: BaseTableViewCell {
    
    @IBOutlet weak var rightBtn: UIButton!
    
    @IBOutlet weak var leftImageView: UIImageView!
    
    @IBOutlet weak var topLabel: UILabel!
    
    @IBOutlet weak var bottomLabel: UILabel!
    
    @IBOutlet weak var bgView: UIView!
    
    @IBOutlet weak var indicatorView: UIActivityIndicatorView!
    
    lazy var starView : StarView = {
        
        let temp = Bundle.main.loadNibNamed(self.StringFromClass(classType: StarView.self), owner: nil, options: nil)?.first as! StarView
        
        
        return temp
        
    }()

    override func awakeFromNib() {
        
        super.awakeFromNib()
        
        contentView.backgroundColor = UIColor.white
        
        bgView.addSubview(starView)
        
        rightBtn.setTitleColor(UIColor.themeColor(), for: .normal)
        
        rightBtn.setTitle("游戏详情", for: .normal)
        
        rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        
        rightBtn.addCornerAndBorder(borderColor: UIColor.themeColor(), borderWidth: 0.6, masksToBounds: true, cornerRadius: 3)

        
    }
    
    
    
    var model : GameListModel?{
    
    
    
        didSet{
            
            if model == nil {
                return
            }
            
            topLabel.text = model?.game_name
            
            
            bottomLabel.attributedText = attributedString(str: "房卡: \(model!.game_card_price)元/张", font: UIFont.systemFont(ofSize: 13))
            
            if let temp = Int(model!.game_star){
                
                starView.starCount = temp
                
                
            }
            
            
            
            indicatorView.startAnimating()
            leftImageView.sd_setImage(with: URL(string: model!.game_logo), placeholderImage: UIImage.defaulImage(), options: []) { (image, _, _, _) in
                self.indicatorView.stopAnimating()
                
                
                
            }
            
            
            
        }
    
    
    }
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        starView.frame = bgView.bounds
        
        
        
        
    }
    
    
    
    
    func attributedString(str: String, font: UIFont) -> NSAttributedString? {
        
        
        let patten0 = "[0-9]{1,15}"
        //创建正则表达式对象
        let patten1 = "[.]"
        
        //        let patten2 = "[¥]"    + "|" +  patten2
        
        let patten = patten0 + "|" + patten1
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res1 = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.dakGray(), range:NSMakeRange(0, str.characters.count) )
        
        strM.addAttribute(NSFontAttributeName, value: font, range:NSMakeRange(0, str.characters.count))
        
        
        for temp in res1{
            
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range:temp.range )
            
            
            
            
        }
        
        
        
        
        return strM
        
        
        
        
        
        
    }

    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
